// Copyright 2012, Cheng Chu.  All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.

// Author: Cheng Chu (zunceng at gmail dot com)
#include <iostream>
#include <boost/function.hpp>
#include "Server.hpp"

struct GlobalServer 
{
	ClientServer* _cs;
	SolveServer* _ss;

	void regist_server(ClientServer* cs,SolveServer* ss)
	{
		_cs = cs;
		_ss = ss;
	}
};

GlobalServer global_server;


void slove_func_recv(Msg& msg)
{
	global_server._cs->write(msg);
}

void client_func_recv(Msg& msg)
{
	global_server._ss->write(msg);
}

//-----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	try
	{
		if (argc < 3)
		{
			std::cerr << "Usage: dbmaster <slove listenning port> <client listening port>\n";
			return 1;
		}
      
		boost::asio::ip::tcp::endpoint slove_listening_endpoint(
			boost::asio::ip::tcp::v4(), std::atoi(argv[1]));

		boost::asio::ip::tcp::endpoint client_listening_endpoint(
			boost::asio::ip::tcp::v4(), std::atoi(argv[2]));

		boost::asio::io_service io_service;

		SolveServer solve_server(io_service, slove_listening_endpoint, slove_func_recv);

		ClientServer client_server(io_service, client_listening_endpoint, client_func_recv);

		global_server.regist_server(&client_server, &solve_server);

		io_service.run();
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
	}


	try
	{
	}
	catch(std::exception &e)
	{
	}
}